package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.enums.OrgTypeEnum;
import com.dongjiantong.user.mapper.OrganizationMapper;
import com.dongjiantong.user.model.Organization;
import com.dongjiantong.user.model.OrganizationUser;
import com.dongjiantong.user.service.OrganizationService;
import com.dongjiantong.user.service.OrganizationUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author cao
 * @create 2017-11-14 下午4:35
 **/
@Slf4j
@Service(version = "1.0.0")
public class OrganizationServiceImpl extends ServiceMybatis<Organization> implements OrganizationService {

    @Resource
    private OrganizationMapper organizationMapper;

    @Autowired
    private OrganizationUserService organizationUserService;


    @Override
    public int createOrganization(Organization organization) {
        return organizationMapper.insert(organization);
    }

    /**
     * 根据单位查询部门
     * @param orgID
     * @return
     */
    @Override
    public List<Organization> findDepByOrgId(long orgID) {
        Organization organization = new Organization();
        organization.setOrgType(OrgTypeEnum.OrgType_department.getCode());
        organization.setParentId(orgID);
        return organizationMapper.select(organization);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> findByUserId(Long id) {
        return organizationMapper.findByUserId(id);
    }

    @Override
    public List<Map<String, Object>> findByArea(Map<String, Object> map) {
        return organizationMapper.findByArea(map);
    }

    @Override
    public Organization getOrganizationById(long id) {
        return organizationMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delOrganizationById(long id) {
        //检查是否有使用,如果有使用不能删除
        List<OrganizationUser> list = organizationUserService.getListByOrgId(id);

        if (list != null && list.size() > 0) {
            return 0;
        }

        //正式删除

        return organizationMapper.deleteByPrimaryKey(id);
    }


}
